﻿package learnmath.windows.elements{
/*-------------------------------------------------------------
	Created by: Ionel Alexandru 
	Mail: ionel.alexandru@gmail.com
	Site: www.learn-math.info
---------------------------------------------------------------*/
import learnmath.windows.*;
import learnmath.windows.apps.*;
import learnmath.windows.util.*;
import learnmath.windows.elements.*;
import learnmath.windows.elements.buttons.*;
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.*;
import flash.net.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;

public class WindowsManager{

	var pannel:MovieClip;
	var taskBar:TaskBar;
	
	var pannelNextX = 100;
	var pannelNextY = 100;	
	var winWidth = 450;
	var winHeight = 300;
	
	var listWindows:Array;
	var listButtons:Array;
	var lastActiveWindowsId:Number =-1;
	
	var resizeAppId = -1;
	var mouseType:String;;
	var mouseCursor:MovieClip;
	var resizeClip = false;
	var resizeProtectMc:MovieClip;
	var resizeMc:MovieClip;
	var leftUpperPoint:Point;
	var rightDownPoint:Point;
	
	var viewDesk = false;
	var angle = -70;
	
	var myTweenRotation1:Tween

	public function WindowsManager(_taskBar:TaskBar){
		pannel = AppManager.mainPannel.workingPannel;
		taskBar = _taskBar;
		listWindows = new Array();
		listButtons = new Array();
		lastActiveWindowsId=-1;
		if(AppManager.mainPannel.width>200){
			winWidth = AppManager.mainPannel.width-100;
			winHeight = AppManager.mainPannel.height-200;
		}
		pannel.addEventListener(MouseEvent.MOUSE_MOVE, changeMouse);
		pannel.addEventListener(MouseEvent.MOUSE_DOWN, beginResize);
		pannel.addEventListener(MouseEvent.MOUSE_UP, endResize);
	}
	
	function beginResize(event:MouseEvent):void {
		if(mouseCursor==null){ return;}
		activate(resizeAppId);

		resizeClip = true;
		var window = listWindows[resizeAppId];
		leftUpperPoint = new Point(window.pannel.x, window.pannel.y);
		rightDownPoint = new Point(window.pannel.x + window.width, window.pannel.y + window.height);
		if(resizeMc!=null){
			pannel.removeChild(resizeMc);
		}
		if(resizeProtectMc!=null){
			pannel.removeChild(resizeProtectMc);
		}
		resizeProtectMc = new MovieClip();
		resizeProtectMc.graphics.lineStyle(1,0xd4cfc9);
		resizeProtectMc.graphics.beginFill(0xd4cfc9);   
		resizeProtectMc.graphics.drawRect(0, 0, AppManager.mainPannel.width-1, AppManager.mainPannel.height-1);   
		resizeProtectMc.graphics.endFill();
		resizeProtectMc.alpha  = 0.1;
		pannel.addChild(resizeProtectMc);
		
		resizeMc = new MovieClip();
		resizeMc.mouseEnabled = false;
		pannel.addChild(resizeMc);
		pannel.removeChild(mouseCursor);
		pannel.addChild(mouseCursor);
	}

	function endResize(event:MouseEvent):void {
		if(mouseCursor==null) return;
		if(resizeMc!=null){
			var point = new Point(mouseCursor.x + 12, mouseCursor.y + 12);

			var window = listWindows[resizeAppId];
			if(mouseType=="nwresize"){
				window.setX(point.x);
				window.setY(point.y);
				window.setWidth(rightDownPoint.x-point.x);
				window.setHeight(rightDownPoint.y-point.y);
			}else if(mouseType=="neresize"){
				window.setY(point.y);
				window.setWidth(point.x-leftUpperPoint.x);
				window.setHeight(rightDownPoint.y - point.y);
			}else if(mouseType=="seresize"){
				window.setWidth(point.x-leftUpperPoint.x);
				window.setHeight(point.y-leftUpperPoint.y);
			}else if(mouseType=="swresize"){
				window.setX(point.x);
				window.setWidth(rightDownPoint.x-point.x);
				window.setHeight(point.y-leftUpperPoint.y);
			}else if(mouseType=="nresize"){
				window.setY(point.y);
				window.setWidth(rightDownPoint.x-leftUpperPoint.x);
				window.setHeight(rightDownPoint.y - point.y);
			}else if(mouseType=="eresize"){
				window.setWidth(point.x-leftUpperPoint.x);
				window.setHeight(rightDownPoint.y-leftUpperPoint.y);
			}else if(mouseType=="sresize"){
				window.setWidth(rightDownPoint.x-leftUpperPoint.x);
				window.setHeight(point.y-leftUpperPoint.y);
			}else if(mouseType=="wresize"){
				window.setX(point.x);
				window.setWidth(rightDownPoint.x-point.x);
				window.setHeight(rightDownPoint.y-leftUpperPoint.y);
			}
			window.draw();
		}
		
		resizeClip = false;
		resetMouse();
		if(resizeMc!=null){
			pannel.removeChild(resizeMc);
		}
		if(resizeProtectMc!=null){
			pannel.removeChild(resizeProtectMc);
		}
		resizeProtectMc = null;
		resizeMc = null;
	}

	function changeMouse(event:MouseEvent):void {
		if(mouseCursor!=null){
			var point:Point = new Point(event.stageX, event.stageY);
			point = pannel.globalToLocal(point);

			if(resizeClip && resizeMc!=null){
				resizeMc.graphics.clear();
				resizeMc.graphics.lineStyle(1,0xd4cfc9);
				if(mouseType=="nwresize"){
					if((rightDownPoint.x-point.x)<100 || (rightDownPoint.y-point.y)<50) return;
					resizeMc.graphics.drawRect(point.x, point.y, rightDownPoint.x-point.x,  rightDownPoint.y-point.y);  
				}else if(mouseType=="neresize"){
					if((point.x-leftUpperPoint.x)<100 || (rightDownPoint.y - point.y)<50) return;
					resizeMc.graphics.drawRect(leftUpperPoint.x, point.y, point.x-leftUpperPoint.x,  rightDownPoint.y - point.y);  
				}else if(mouseType=="seresize"){
					if((point.x-leftUpperPoint.x)<100 || (point.y-leftUpperPoint.y)<50) return;
					resizeMc.graphics.drawRect(leftUpperPoint.x, leftUpperPoint.y, point.x-leftUpperPoint.x,  point.y-leftUpperPoint.y);  
				}else if(mouseType=="swresize"){
					if((rightDownPoint.x-point.x)<100 || (point.y-leftUpperPoint.y)<50) return;
					resizeMc.graphics.drawRect(point.x, leftUpperPoint.y, rightDownPoint.x-point.x,  point.y-leftUpperPoint.y);  
				}else if(mouseType=="nresize"){
					if((rightDownPoint.x-leftUpperPoint.x)<100 || (rightDownPoint.y - point.y)<50) return;
					resizeMc.graphics.drawRect(leftUpperPoint.x, point.y, rightDownPoint.x-leftUpperPoint.x,  rightDownPoint.y - point.y);  
				}else if(mouseType=="eresize"){
					if((point.x-leftUpperPoint.x)<100 || (rightDownPoint.y-leftUpperPoint.y)<50) return;
					resizeMc.graphics.drawRect(leftUpperPoint.x, leftUpperPoint.y, point.x-leftUpperPoint.x,  rightDownPoint.y-leftUpperPoint.y);  
				}else if(mouseType=="sresize"){
					if((rightDownPoint.x-leftUpperPoint.x)<100 || (point.y-leftUpperPoint.y)<50) return;
					resizeMc.graphics.drawRect(leftUpperPoint.x, leftUpperPoint.y, rightDownPoint.x-leftUpperPoint.x,  point.y-leftUpperPoint.y);  
				}else if(mouseType=="wresize"){
					if((rightDownPoint.x-point.x)<100 || (rightDownPoint.y-leftUpperPoint.y)<50) return;
					resizeMc.graphics.drawRect(point.x, leftUpperPoint.y, rightDownPoint.x-point.x,  rightDownPoint.y-leftUpperPoint.y);  
				}
			}
			mouseCursor.x = point.x -12;
			mouseCursor.y = point.y -12;

		}
	}
	
	function setMouse(_mouseType:String, event:MouseEvent, appId:int):void {
		if(resizeClip) return;
		if(mouseType==_mouseType) return;
		
		mouseType = _mouseType;
		if(mouseType=="nwresize"){
			mouseCursor = new nwresizeMc();
		}else if(mouseType=="neresize"){
			mouseCursor = new neresizeMc();
		}else if(mouseType=="seresize"){
			mouseCursor = new seresizeMc();
		}else if(mouseType=="swresize"){
			mouseCursor = new swresizeMc();
		}else if(mouseType=="nresize"){
			mouseCursor = new nresizeMc();
		}else if(mouseType=="eresize"){
			mouseCursor = new eresizeMc();
		}else if(mouseType=="sresize"){
			mouseCursor = new sresizeMc();
		}else if(mouseType=="wresize"){
			mouseCursor = new wresizeMc();
		}
		resizeAppId = appId;
		Mouse.hide();
		mouseCursor.mouseEnabled = false;
		mouseCursor.mouseChildren = false;
		pannel.addChild(mouseCursor);
		changeMouse(event);
	}

	function resetMouse():void {
		if(resizeClip){ return;	}
		
		if(mouseCursor!=null){
			pannel.removeChild(mouseCursor);
		}
		mouseCursor = null;
		mouseType=null;
		Mouse.show();
	}
	
	public function createAplication(id:String):void{
		var apps:Array = ConfigManager.applications;
		for(var i=0; i<apps.length; i++){
			var appbean:AppBean = apps[i];
			if(id==appbean.id){
				createWindowForApp(appbean);
				break;
			}
		}	
	}
	
	public function createWindowForApp(appbean:AppBean):void{
		//desactive all other window
		for(var i=0; i<listWindows.length; i++){
			if(listWindows[i]!=null){
				var w = listWindows[i];
				w.setActivateWindow(false);
				
				if(appbean.singleton && w.id==appbean.id){
					activate(w.pannel.appId);
					return;
				}
			}
		}

		var win:WindowXP;
		if(appbean.id=="MathML-Editor"){
			win = new EditorApp(this, pannel, appbean.getIconLittleMc(), appbean.name,  appbean.id, pannelNextX, pannelNextY, winWidth, winHeight, appbean.movieclip);
		}else if(appbean.id=="MathML-Graph"){
			win = new GraphApp(this, pannel, appbean.getIconLittleMc(), appbean.name,  appbean.id, pannelNextX, pannelNextY, winWidth, winHeight, appbean.movieclip);
			GraphApp(win).addFunction("<mrow><mn>5</mn><mi>x</mi><mo>+</mo><mn>5</mn></mrow>");
			GraphApp(win).addFunction("<mrow><mi>x</mi><mi>x</mi><mo>-</mo><mn>2</mn><mi>x</mi><mo>+</mo><mn>1</mn></mrow>");
			GraphApp(win).addFunction("<mrow><mi>x</mi><mi>x</mi><mi>x</mi><mo>-</mo><mi>x</mi><mi>x</mi><mo>+</mo><mn>2</mn><mi>x</mi><mo>-</mo><mn>1</mn></mrow>");
			GraphApp(win).addFunction("<mrow><msup><mi>x</mi><mn>4</mn></msup><mo>-</mo><mi>x</mi><mi>x</mi><mo>+</mo><mn>2</mn><mi>x</mi><mo>-</mo><mn>1</mn></mrow>");
		}else if(appbean.id=="XML-Editor"){
			win = new XmlEditorApp(this, pannel, appbean.getIconLittleMc(), appbean.name,  appbean.id, pannelNextX, pannelNextY, winWidth, winHeight, appbean.movieclip);
		}else if(appbean.appTypeId=="HelpViewer"){
			
			win = new HelpApp(this, pannel, appbean.getIconLittleMc(), appbean.name,  appbean.id, pannelNextX, pannelNextY, winWidth, winHeight, appbean.movieclip);
		}else{
			win = new WindowXP(this, pannel, appbean.getIconLittleMc(), appbean.name,  appbean.id, pannelNextX, pannelNextY, winWidth, winHeight);
		}
		
		configWindow(win, appbean)
	}
	
	public function configWindow(win:WindowXP, appbean:AppBean):WindowXP{
		listWindows[listWindows.length] = win;
		for(var i=0; i<listButtons.length; i++){
			if(listButtons[i]!=null){
				listButtons[i].drawUP();
			}
		}
		
		var button:ButtonTextImgWidthXP = taskBar.addButton(appbean.getIconLittleMc(), appbean.name);
		listButtons[listButtons.length] = button;

		lastActiveWindowsId=listWindows.length-1;
		button.pannel.appId = listWindows.length-1;
		button.pannel.addEventListener(MouseEvent.CLICK, activateWindow);

		win.pannel.appId = listWindows.length-1;
		win.pannel.addEventListener(MouseEvent.CLICK, activateWindow);
		win.draw();
		win.pannel.x = 50 + (listWindows.length-1)*22;
		win.pannel.y = 50 + (listWindows.length-1)*22;
		activate(win.pannel.appId);
		return win;
	}
	
	function addListenerToButtons(win:WindowXP):void {
		win.titleBar.appId = win.pannel.appId;
		win.bMin.pannel.appId = win.pannel.appId;
		win.bMin.pannel.addEventListener(MouseEvent.CLICK, minimizeWindow);
		win.bClose.pannel.appId = win.pannel.appId;
		win.bClose.pannel.addEventListener(MouseEvent.CLICK, closeWindow);
	}

	function closeWindow(event:MouseEvent):void {
		closeWindowId(event.target.appId)
		event.stopPropagation();
	}	

	public function closeWindowId(windowId:int):void {
		var window = listWindows[windowId];

		pannel.removeChild(window.pannel);
		listWindows[windowId] = null;
		
		taskBar.removeButton(windowId);
		listButtons[windowId] = null;
		activateNextWindow(windowId);
	}	

	public function viewDesktop():void {
		if(viewDesk){
			for(var i=0; i<listWindows.length; i++){
				if(listWindows[i]!=null){
					var window = listWindows[i];
					window.status = "NORMAL";
					window.pannel.visible = true;
					window.pannel.rotationY = angle;
					window.setActivateWindow(false);
				}
			}
			AppManager.mainPannel.iconsManager.bringIconsInFront();
			
		}else{
			for(var i=0; i<listWindows.length; i++){
				if(listWindows[i]!=null){
					var window = listWindows[i];
					window.status = "MINIMIZED";
					window.pannel.visible = false;
				}
			}
			for(i=0; i<listButtons.length; i++){
				if(listButtons[i]!=null){
					listButtons[i].drawUP();
				}
			}
		}
		viewDesk = !viewDesk;
	}	

	function minimizeWindow(event:MouseEvent):void {
		var window = listWindows[event.target.appId];
		activateNextWindow(event.target.appId);

		window.status = "MINIMIZED";
		window.pannel.visible = false;
		listButtons[event.target.appId].drawUP();
		event.stopPropagation();
	}	
	
	
	function activateWindow(event:MouseEvent):void {
		var appId = 0;
		var o = event.target;
		
		while(true){
			if(o is MovieClip && o.appId!=null) break;
			o = o.parent;
			if(o==null) break;
			if(o is MovieClip && o.appId!=null) break;
		}
		if(o!=null){
			activate(o.appId);
		}
		event.stopPropagation();
	}	
	
	function activate(appId:Number):void {
		var window = listWindows[appId];
		var button = listButtons[appId];
		for(var i=0; i<listButtons.length; i++){
			if(i==appId) continue;
			if(listButtons[i]!=null){
				listButtons[i].drawUP();
			}
		}
		button.drawDOWN();
		window.pannel.visible = true;
		
		var pannels = 0;
		for(i=0; i<listWindows.length; i++){
			if(i==appId) continue;
			var w = listWindows[i];
			if(w==null) continue;
			w.setActivateWindow(false);
			w.pannel.rotationY = angle;
			pannels++;
		}
		AppManager.mainPannel.iconsManager.bringIconsInFront();
		
		if(window.pannel.rotationY!=0){
			if(myTweenRotation1!=null && myTweenRotation1.isPlaying){
				myTweenRotation1.stop();
			}
			myTweenRotation1 = new Tween(window.pannel, "rotationY", Strong.easeOut, window.pannel.rotationY, 0, 3, true);
		}
		window.setActivateWindow(true);
		pannel.removeChild(window.pannel);
		pannel.addChild(window.pannel);
		lastActiveWindowsId=appId;
	}

	function activateNextWindow(appId):void {
		var nextId = -1;
		for(var i=(appId+1); i<listWindows.length; i++){
			if(listWindows[i]!=null && listWindows[i].status != "MINIMIZED" ){
				nextId = i;
				break;
			}
		}
		
		if(nextId==-1){
			for(i=0; i<appId; i++){
				if(listWindows[i]!=null && listWindows[i].status != "MINIMIZED"){
					nextId = i;
					break;
				}
			}
		}
		if(nextId>-1){
			activate(nextId);
		}
	}

	public function drawOnFrame():void{
		if(lastActiveWindowsId<0) return;
		if(listWindows.length<=lastActiveWindowsId) return;
		if(listWindows[lastActiveWindowsId]==null) return;
		listWindows[lastActiveWindowsId].drawOnFrame();
	}

	public function processKey(event:KeyboardEvent):void {
		if(lastActiveWindowsId<0) return;
		if(listWindows.length<=lastActiveWindowsId) return;
		if(listWindows[lastActiveWindowsId]==null) return;
		listWindows[lastActiveWindowsId].processKey(event);
	}	

}
}